package com.giga.controller;

import com.giga.entity.User;
import com.giga.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 前台客户订阅页面
     */
    @RequestMapping(value = {"/", "/index"})     //窄化映射请求
    public Object index() {
        return "index";
    }

    /**
     * 前台客户订阅信息的填写页面
     */
    @RequestMapping("/subscribe")
    public Object subscribe() {
        return "subscribe";
    }

    /**
     * 前台客户订阅，插入客户数据
     */
    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    @ResponseBody
    public int add(User user) {
        int result = 0;
        if (user.getName() != null && user.getName().trim().length() > 0 && user.getEmail() != null && user.getEmail().trim().length() > 0) {
            User user1 = userService.selectByName(user);
            if (user1 == null) {    //订阅注册时根据邮箱没有查询user————表示数据库不存在注册时的邮箱，可以添加
                result = userService.insertUser(user);
                System.out.println("add==============================success");
            }
        }
        return result;
    }

//    @RequestMapping("/getUserEmail")
//    @ResponseBody
//    public Object getUserEmail(String email){
//        int result=0;
//        User u=new User();
//        u.setEmail(email);
//        User user = userService.selectByName(u);
//        if (user !=null){
//            result=1;
//        }
//        return result;
//    }
}
